#include "Include.h"

#include "SoundManager.h"
#include "Rectangle.h"
#include "Character.h"
#include "ImageManager.h"
//#include "TextManager.h"
#include "System.h"
#include "Item.h"
#include "Map/Map.h"
#include "Ui/Manager.h"
#include "Ui/Widget/Label.h"
#include "Ui/Widget/Button.h"
#include "Ui/Widget/LineEdit.h"
#include "Ui/Widget/Slider.h"
#include "Ui/Widget/CheckBox.h"
#include "Ui/Widget/RadioBox.h"
//#include "TextArea.h"

using namespace sf;
using namespace std;

/**
 * @name Luke Fangel
 * collision function
 ***/

bool Collision(const core::Rectangle& A, const core::Rectangle& B) {
    return (A.x < B.x + B.w &&
            A.x + A.w > B.x &&
            A.y < B.y + B.h &&
            A.y + A.h > B.y);

}
/// * :D

int main() {
    System system;
    ImageManager& imgMgr = ImageManager::GetSingleton();
    imgMgr.Initialize();
    SoundManager sndMgr;
    //TextManager txtMgr;
    Character player(imgMgr.GetImage("player"));
    Item stick(imgMgr.GetImage("stick"));

    srand(time(0));

    stick.SetRange(system.GetScreenWidth(), system.GetScreenHeight());
    stick.Collect(); // sets it to a random spot
    PickinOpenSticks::Map::Map map;
    map.Initialize(imgMgr.GetImage(3));

    //init gui
    PickinOpenSticks::Ui::Label label;
    PickinOpenSticks::Ui::Label label2;
    label.SetPos(300, 0);
    label2.SetPos(450, 0);
    label.HideBackground();
    label2.HideBackground();
    PickinOpenSticks::Ui::Button exitButton;
    exitButton.SetDefaultImage();
    exitButton.SetPos(550, 500);
    exitButton.SetText("Exit");

    PickinOpenSticks::Ui::Widget mainPlayWidget;
    mainPlayWidget.SetDefaultImage();
    mainPlayWidget.Resize(system.GetScreenWidth(), system.GetScreenHeight());
    mainPlayWidget.HideBackground();
    mainPlayWidget.SetPos(0, 0);
    PickinOpenSticks::Ui::Manager::GetSingleton().AddWidget("mainPlayWidget", &mainPlayWidget);
    mainPlayWidget.AddWidget(&label);
    mainPlayWidget.AddWidget(&label2);
    mainPlayWidget.AddWidget(&exitButton);
    mainPlayWidget.SetMovable(false);


    //start game label
    PickinOpenSticks::Ui::Label main;
    main.SetDefaultImage();
    main.text.SetText("Start Game");
    main.SetName("startGameLabel");
    main.SetPos(300, 250);

    //main menu widget --dummy widget
    PickinOpenSticks::Ui::Widget mainWidget;
    mainWidget.SetDefaultImage();
    mainWidget.Resize(system.GetScreenWidth(), system.GetScreenHeight());
    mainWidget.HideBackground();
    mainWidget.SetPos(0, 0);
    mainWidget.SetMovable(false);
    PickinOpenSticks::Ui::Manager::GetSingleton().AddWidget("mainMenuWidget", &mainWidget);

    //add widgets to dummy widget
    mainWidget.AddWidget(&main);
    
    // Setup a clock for fps display
    sf::Clock fpsClock;

    while (system.GetState() != EXIT) {

        switch (system.GetState()) {
            case MENU:
                system.CheckEvents();
                //draw menu
            {
                mainPlayWidget.Hide();
                mainWidget.Show();
                if (main.ClickReleased())
                    system.SetState(RUNNING);

                system.BeginDraw();
                PickinOpenSticks::Ui::Manager::GetSingleton().Update(0);
                system.EndDraw();
            }
                break;
            case RUNNING:
            {
                mainWidget.Hide();
                mainPlayWidget.Show();

                if (exitButton.ClickReleased())
                    system.SetState(MENU);

                system.CheckEvents();
                system.HandleKeyboardInput(player);

                if (Collision(player.GetRect(), stick.GetRect())) {
                    stick.Collect();
                    player.IncrementScore();
                    sndMgr.PlaySound("pick");
                }
                
                // Draw to screen
                system.BeginDraw();
                map.Render(system);
                system.Draw(player.GetSprite());
                system.Draw(stick.GetSprite());
                // Draw score
                //txtMgr.DisplayText( system.program, player.GetScoreString(), 0, 0 );
                //txtMgr.DisplayText( system.program, player.GetRank(), 125, 0 );
                label.text.SetText(player.GetScoreString());
                label2.text.SetText(player.GetRank());

                // Render FPS
                if (fpsClock.GetElapsedTime() > 1.0) {
                    cout << "FPS: " << system.GetFramesPerSecond() << endl;
                    fpsClock.Reset();
                }

                PickinOpenSticks::Ui::Manager::GetSingleton().Update(0);
                system.EndDraw();
                break;
            }
            case PAUSED:
            default:
                break;
        }
    } // while ( system.GetState() == RUNNING )

    return 0;
} // int main()
